//
//  ReadABView.m
//  LineByLine
//
//  Created by 병철 김 on 11. 10. 4..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "PairTableViewCell.h"
#import "CommonDefine.h"


@implementation PairTableViewCell
@synthesize sentence, rowId, col, viewDelegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        pairView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        [pairView setPagingEnabled:YES];
        [pairView setShowsHorizontalScrollIndicator:NO];
        [pairView setDelegate:self];
        
        UIView *left = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, kReadViewHeight)];
        UIView *right = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, kReadViewHeight)];
        
        [left setBackgroundColor:[UIColor clearColor]];
        [right setBackgroundColor:[UIColor clearColor]];
        
        leftText = [[ShadowedTextViewGroup alloc] initWithFrame:CGRectMake(35, 40, 270, 260)];
        rightText = [[ShadowedTextView alloc] initWithFrame:CGRectMake(35, 40, 260, 260)];
        
        [left addSubview:leftText];
        [right addSubview:rightText];
        
        [pairView addSubview:left];
        [pairView addSubview:right];
        
        [leftText release];
        [rightText release];
        [left release];
        [right release];
        
        [self.contentView addSubview:pairView];
        [pairView release];
    }
    return self;
}

- (void) configView {
    CGRect frame = self.frame;
    [pairView setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [pairView setContentSize:CGSizeMake(frame.size.width * 2, frame.size.height)];
    [pairView setContentOffset:CGPointMake(col * frame.size.width, 0)];
    
    [leftText setDelegate:viewDelegate];
    [leftText setText:[sentence valueForKey:@"a"]];
    [rightText setText:[sentence valueForKey:@"b"]];
}

#pragma mark - UIScrollViewDelegate
- (void) calcCurCol {
    CGPoint offset = pairView.contentOffset;
    int newCol;
    if (offset.x >= (self.frame.size.width/2)) {
        newCol = 1;
    } else {
        newCol = 0;
    }
    
    if (col != newCol) {
        col = newCol;
        [viewDelegate colDidChanged:col];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [viewDelegate onClickText:nil];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self calcCurCol];
}

@end
